package com.zhangyj.impl.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class IbatisDao {
	protected SqlSessionFactory factory;

	public IbatisDao(SqlSessionFactory factory) {
		this.factory = factory;
	}

	SqlSession getSqlSession() {
		return factory.openSession(ExecutorType.BATCH);
	}

	public int delete(String statement, Object parameter) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.delete(statement, parameter);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}
	

	public int delete(String statement) {
		return delete(statement,null);
	}

	public int insert(String statement, Object parameter) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.insert(statement, parameter);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public int insert(String statement) {
		return this.insert(statement, null);
	}

	public List<?> selectList(String statement, Object parameter, RowBounds rowBounds) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.selectList(statement, parameter, rowBounds);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public List<?> selectList(String statement, Object parameter) {
		return this.selectList(statement, parameter, null);
	}

	public List<?> selectList(String statement) {
		return this.selectList(statement, null, null);
	}

	public Map<?, ?> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.selectMap(statement, parameter, mapKey, rowBounds);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public Map<?, ?> selectMap(String statement, Object parameter, String mapKey) {
		return this.selectMap(statement, parameter, mapKey, null);
	}

	public Map<?, ?> selectMap(String statement, String parameter) {
		return this.selectMap(statement, parameter, null, null);
	}

	public Object selectOne(String statement, Object parameter) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.selectOne(statement, parameter);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public Object selectOne(String statement) {
		return this.selectList(statement,null);
	}

	public int update(String statement, Object parameter) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			return sqlSession.update(statement, parameter);
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}

	public int update(String statement) {
		return this.update(statement, null);
	}
}
